<template>
  <div class="m-window m-view-appointmentList">
      <div class="m-condition m-appointmentList-condition">
        <el-row :gutter="5">
          <el-form
            ref="headerRuleForm"
            label-width="70px"
            :inline-message="true"
            label-position="left"
            class="m-headerForm"
          >
            <el-col :span="2" :offset="6">
              <el-button @click="queryAppointList(1)" type="primary" :disabled="queryDisabled">查询</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="editAppointList" type="primary" :disabled="!(multipleSelection.length == 1)">编辑</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="appointmentPassOrReturn(1)" type="primary" :disabled="!(multipleSelection.length > 0)">预约通过</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="confirmArrivalBtn" type="primary" :disabled="!(multipleSelection.length ==1)">确认进场</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="confirmUnloadingBtn" type="primary" :disabled="!(multipleSelection.length ==1)">卸完确认</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="onSiteBilling" type="primary" :disabled="siteBillDisabled">现场开单</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="print('approvalPrint')" type="primary" :disabled="!(multipleSelection.length ==1)">打印批准书</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="appointmentPassOrReturn(2)" type="primary" :disabled="!(multipleSelection.length > 0)">预约退回</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="print('receiptPrint')" type="primary" :disabled="!(multipleSelection.length ==1)">收条打印</el-button>
            </el-col>
          </el-form>  
        </el-row>
        <el-row :gutter="10">
          <el-form
            ref="headerRuleForm"
            label-width="90px"
            :inline-message="true"
            label-position="left"
            class="m-headerForm"
          >
            <el-col :span="5">
              <el-form-item label="公司名称" class="rightItem" >
                 <el-select
                 @change="companyChange"
                  placeholder="公司名称"
                  v-model="companyCode"
                  clearable>
                    <el-option
                      v-for="(item,index) in companyItem"
                      :key="index"
                      :label="item.companyName"
                      :value="item.companyCode">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>


            <el-col :span="4">
              <el-form-item label="车号" class="rightItem">
                <el-input v-model="carNo" placeholder="车号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="预约时段" class="rightItem">
                 <el-select
                  placeholder="预约时段"
                  v-model="appointPeriod"
                  clearable>
                    <el-option
                      v-for="(item,index) in appointPeriodItem"
                      :key="index"
                      :label="item"
                      :value="item">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="采购订单号" class="rightItem">
                <el-input v-model="purchaseOrderNo" placeholder="采购订单号" @change="purchaseOrderChange" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item class="rightItem" label="预约日期">
                <el-date-picker
                  v-model="startDate"
                  type="date"
                  placeholder="开始日期"
                  value-format="yyyy-MM-dd"
                  clearable>
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-date-picker
                v-model="endDate"
                type="date"
                placeholder="结束日期"
                value-format="yyyy-MM-dd"
                clearable>
              </el-date-picker>
            </el-col>
          </el-form>  
        </el-row>
        <el-row :gutter="10">
          <el-form
            ref="headerRuleForm"
            label-width="90px"
            :inline-message="true"
            label-position="left"
            class="m-headerForm"
          >   
            <el-col :span="5">
              <el-form-item label="供应商名称" class="rightItem">
                <el-input v-model="supplier" placeholder="供应商名称" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="柜号/卡号" class="rightItem">
                <el-input v-model="cabinetNoAndCardNo" placeholder="集装箱柜号/铁路卡号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="卸货地点" class="rightItem">
                 <el-select
                  placeholder="卸货地点"
                  v-model="unloadLocation"
                  clearable>
                    <el-option
                      v-for="(item,index) in unloadLocationItem"
                      :key="index"
                      :label="item.unloadAddree"
                      :value="item.unloadAddree">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="运输方式" class="rightItem">
                 <el-select
                  placeholder="运输方式"
                  v-model="transportType"
                  clearable>
                    <el-option
                      v-for="(item,index) in transportTypeItem"
                      :key="index"
                      :label="item"
                      :value="item">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="状态" class="rightItem">
                 <el-select
                  placeholder="状态"
                  v-model="appointState"
                  clearable>
                    <el-option
                      v-for="(item,index) in appointStateItem"
                      :key="index"
                      :label="item.name"
                      :value="item.code">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
           
          </el-form>  
        </el-row>
      </div>
      <!-- 表格数据 -->
      <div class="m-agTable m-bottom " >
        <ag-grid-vue class="ag-theme-balham ag-table"
            :gridOptions="gridOptions"
            :columnDefs="columnDefs"
            :rowData="rowData"
            v-loading="loading"
            @cellFocused="rowFocus"
            @selection-changed="handleSelectionChange"
            @rowDoubleClicked="doubleClickedEdit">
        </ag-grid-vue>
      </div>
      <!-- 分页 -->
      <div class="m-page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @prev-click="handlePrevClick"
          @next-click="handleNextClick"
          :current-page="currentPage"
          :page-sizes="[30,50,100,150,200]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
        ></el-pagination>
      </div>
      <!-- 弹出框 -->
      <el-dialog 
        :title="dialogTitle" 
        :visible.sync="dialogFormVisible" 
        :before-close="dialogCancel"
        v-dialogDrag>
        <el-form :model="appointform" label-width="150px" ref="appointform">
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="车牌号码" prop="carNum">
                <el-input v-model="appointform.carNum" placeholder="车牌号码" ref="carNum" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="司机姓名" prop="driver">
                <el-input v-model="appointform.driver" placeholder="司机姓名" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="运输方式" prop="transport">
                <el-select v-model="appointform.transport" placeholder="运输方式" clearable>
                  <el-option 
                  v-for="(item,index) in transportTypeItem"
                  :key="index"
                  :value="item"
                  :label="item">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="司机号码" prop="driverPhone">
                <el-input v-model="appointform.driverPhone" placeholder="司机号码" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="采购订单" prop="purchaseOrderNo">
                <el-input v-model="appointform.purchaseOrderNo" placeholder="采购订单" clearable @change="purchaseOrderChange" @clear="purchaseOrderClear"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-input v-model="appointform.supplierName" placeholder="供应商名称" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="卡号/柜号" prop="cabinet">
                <el-input v-model="appointform.cabinet" placeholder="集装箱柜号/铁路卡号" clearable></el-input>
                
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="送货数量" prop="quantity">
                <el-input v-model="appointform.quantity" placeholder="送货数量(kg)" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="货物名称" prop="goodsName">
                <el-input v-model="appointform.goodsName" placeholder="货物名称(100字内)" ref="goodsName" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预约方式" prop="appointmentMode">
                <el-input v-model="appointform.appointmentMode" placeholder="预约方式" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="预约时间" prop="appointmentDate">
                <el-date-picker
                  v-model="appointform.appointmentDate"
                  type="date"
                  placeholder="预约时间"
                  value-format="yyyy-MM-dd"
                  :readonly="appointDateReadOnly"
                  clearable>
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请时间" prop="applicationTime">
                <el-date-picker
                  v-model="appointform.applicationTime"
                  type="datetime"
                  placeholder="申请时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  readonly>
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="公司名称" prop="companyCode">
                <el-select v-model="appointform.companyCode" placeholder="公司名称" @change="companyChange" @clear="clearCompaney" clearable>
                  <el-option 
                    v-for="(item,index) in companyItem"
                    :key="index"
                    :value="item.companyCode"
                    :label="item.companyName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="剩余数量" prop="residueAmount">
                <el-input v-model="appointform.residueAmount" placeholder="剩余数量(kg)" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='siteBill'">
            <el-col :span="12">
              <el-form-item label="卸货地点" prop="unloadingPlace">
                <el-select v-model="appointform.unloadingPlace" placeholder="卸货地点" @change="addressChange" clearable>
                  <el-option 
                  v-for="(item,index) in unloadLocationItem"
                  :key="index"
                  :value="item.unloadAddree"
                  :label="item.unloadAddree">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="联系人:" prop="linkMan">
                    <el-select v-model="appointform.linkMan" placeholder="卸货地点联系人" clearable>
                        <el-option
                            v-for="(item,index) in areaLinkMans"
                            :key="index"
                            :value="item.linkman"
                            :label="item.linkman"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
          </el-row>
           <el-row v-if="dialogEdit=='confirmUnload'">
            <el-col :span="12">
              <el-form-item label="送货包数" prop="deliveryPacket">
                <el-input v-model="appointform.deliveryPacket" placeholder="送货包数" clearable ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="破包数量" prop="brokenPacket">
                <el-input v-model="appointform.brokenPacket" placeholder="破包数量" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='confirmUnload'" >
            <el-col :span="12" >
              <el-form-item label="实收吨数" prop="receiptsQty">
                <el-input v-model="appointform.receiptsQty" placeholder="实收吨数" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="共欠数量" prop="debtQty">
                <el-input v-model="appointform.debtQty" placeholder="共欠数量" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="dialogEdit=='edit'||dialogEdit=='confirmGoIn'||dialogEdit=='confirmUnload'">
            <el-col :span="12">
              <el-form-item label="过磅重量(kg)" prop="weighingWeight">
                <el-input v-model="appointform.weighingWeight" placeholder="过磅重量(kg)" clearable :readonly="weighingReadonly"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="皮重(kg)" prop="tareWeight">
                <el-input v-model="appointform.tareWeight" @input="inputKeyup" placeholder="皮重(kg)" clearable :readonly="tareReadonly"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row >
            <el-col :span="12">
              <el-form-item label="净重(kg)" prop="netWeight" v-show="dialogEdit=='edit' ||dialogEdit=='confirmUnload'">
                <el-input v-model="appointform.netWeight"  placeholder="净重(kg)" clearable :readonly="netReadonly"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预约时段" prop="appointmentPeriod">
                <el-select v-model="appointform.appointmentPeriod" placeholder="预约时段" clearable>
                  <el-option 
                    v-for="(item,index) in appointPeriodItem"
                    :key="index"
                    :value="item"
                    :label="item">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
           <el-row>
            <el-col :span="12" v-if="dialogEdit=='edit'">
              <el-form-item label="状态" prop="state">
                <el-input v-model="appointform.state" placeholder="状态" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="确认时间" v-show="dialogEdit=='edit' " prop="confirmDate">
                <el-input v-model="appointform.confirmDate" placeholder="确认时间" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogCancel">取 消</el-button>
          <el-button type="primary" @click="dialogEditSubmit" v-show="dialogEdit=='edit'" :disabled="editConfirm">保 存</el-button>
          <el-button type="primary" @click="dialogEditSubmit" v-show="dialogEdit=='siteBill'" :disabled="dialogSiteBillSubmit">开 单</el-button>
          <el-button type="primary" @click="confirmArrival" v-show="dialogEdit=='confirmGoIn'" :disabled="confirmGoIn">确认进场</el-button>
          <el-button type="primary" @click="confirmUnloading" v-show="dialogEdit=='confirmUnload'" :disabled="confirmUnload">卸完确认</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>

import gridOptions from "lesso-common/public/gridOptions"
import constant from "@/public/constant"
import fileStream from "@/public/fileStream";
import { startPrint } from 'lesso-common/webprint';
import waterfall from "async-es/waterfall";
import Msg from "@/public/message.js";
import apienv from '@/api/env';
import aes from '@/public/aes';
const ENV = apienv[constant.appenv]
import websocketClient from "lesso-common/public/websocketClient"
/**
 * @group appointmentList
 * 预约卸货
*/
export default {
    name:"appointmentList",
    components:{
        //选择卸货地点
        selectUnloadLocation:{
            template:`<el-select 
                        class="selectStyle" 
                        v-model="unloadLocation"
                        @change="selectChange()"
                        @clear="clearSelect()"
                        placeholder="请选择卸货地点" 
                        clearable>
                        <el-option 
                            v-for="(item,index) in unloadLocationItem" 
                            :key="index" 
                            :label="item.unloadAddree" 
                            :value="item.unloadAddree">
                        </el-option>
                    </el-select>`,
            data () {
                return {
                    unloadLocation:'',       //卸货地点
                    unloadLocationItem:[],   //地点列表
                  
                       
                }
            },
           
            created() {
                let that = this.params.context.componentParent;
                this.unloadLocation=this.params.data.unloadingPlace; //赋值地点
                this.unloadLocationItem=that.unloadLocationInputItem //卸货地点数据
            },
            methods:{
                selectChange(){
                    let that = this.params.context.componentParent;
                    that.rowData[that.selectRowIndex].unloadingPlace=this.unloadLocation;
                    that.gridOptions.api.updateRowData({
                        update:[that.rowData[that.selectRowIndex]]
                    });
                },
                clearSelect(){
                    let that = this.params.context.componentParent;
                    that.rowData[that.selectRowIndex].unloadingPlace="";
                    that.gridOptions.api.updateRowData({
                        update:[that.rowData[that.selectRowIndex]]
                    });
                }
            }
        },

        //选择联系人
        selectLinkMan:{
            template:`<el-select 
                        class="selectStyle" 
                        v-model="linkMan"
                        @change="selectChange()"
                        @clear="clearSelect()"
                        placeholder="请选择联系人" 
                        clearable>
                        <el-option 
                            v-for="(item,index) in linkManItem" 
                            :key="index" 
                            :label="item.linkman" 
                            :value="item.linkman">
                        </el-option>
                    </el-select>`,
            data () {
                return {
                    linkMan:'',        //联系人
                    linkManItem:[],    //联系人列表 
                }
            },
            created() {
                let that = this.params.context.componentParent;
                this.linkMan=this.params.data.linkMan;               //赋值联系人
                this.linkManItem=that.unloadLocationInputItem        //联系人数据
            },
            methods:{
                selectChange(){
                    let that = this.params.context.componentParent;
                    that.rowData[that.selectRowIndex].linkMan=this.linkMan;
                    that.gridOptions.api.updateRowData({
                        update:[that.rowData[that.selectRowIndex]]
                    });
                },
                clearSelect(){
                    let that = this.params.context.componentParent;
                    that.rowData[that.selectRowIndex].linkMan="";
                    that.gridOptions.api.updateRowData({
                        update:[that.rowData[that.selectRowIndex]]
                    });
                }
            }
        }
    },
    data(){
      return {
        queryDisabled:false,               //查询按钮可见
        appointDateReadOnly:false,         //预约时间只读
        weighingReadonly:false,            //磅重只读
        tareReadonly:false,                //皮重只读
        netReadonly:false,                 //净重只读
        siteBillDisabled:false,            //现场开单按钮可见
        printApprovalDisabled:false,       //打印批准书按钮可见
        // appointReturnDisabled:false,    //预约退回按钮可见
        receiptPrintDisabled:false,        //收条打印按钮可见
        dialogSiteBillSubmit:false,        //开单按钮可见
        editConfirm:false,                 //编辑按钮可见
        confirmGoIn:false,                 //确认进场按钮可见
        confirmUnload:false,               //卸完确认按钮可见

        multipleSelection:[],              //选中
        companyCode:"",                    //公司编码
        companyItem:[],                    //公司数据
        areaLinkMans:[],
        // 搜索框数据
        carNo:"",                      //车号
        appointPeriod:"",              //预约时段
        appointPeriodItem:[],           //预约时段数据
        
        purchaseOrderNo:"",            //采购订单号
        transportType:"",              //运输方式
        transportTypeItem:[            //运输方式数据
          "汽车","铁路","集装箱"
        ],
        appointState:"",               //状态
        appointStateItem:[             //状态数据
        {code:"0",name:"未审批"},
        {code:"1",name:"已通过"},
        {code:"2",name:"已退回"},
        {code:"3",name:"已到达"},
        {code:"4",name:"已取消"},
        {code:"5",name:"已卸货"},
        ],                      
        unloadLocation:"",             //卸货地点
        unloadLocationItem:[],         //卸货地点数据                         
        unloadLocationInputItem:[],    //列表卸货地点数据                         
        startDate:"",                  //预约开始日期
        endDate:"",                    //预约结束日期
        supplier:"",                   //供应商
        cabinetNoAndCardNo:"",         //柜号/卡号

        // 列表数据
        gridOptions:null,
        columnDefs:null,
        rowData:[],
        loading:false,

        // 分页
        currentPage:1,                 //当前页
        pageSize:30,                   //每页条数
        totalPage:0,                   //总数


        //弹出框数据
        dialogFormVisible:false,        //弹出框隐藏
        dialogEdit:"edit",              //隐藏输入框
        dialogTitle:"",                 //弹框标题
        appointform:{                   //节假日表格数据
          carNum:"",                    //车牌号码
          driver:"",                    //司机名字
          driverPhone:"",               //手机号码
          transport:"",                 //运输方式
          purchaseOrderNo:"",           //采购订单
          supplierName:"",              //供应商名称
          supplierCode:"",              //供应商代码
          cabinet:'',                   //卡号/柜号
          quantity:'',                  //送货数量
          goodsName:'',                 //货物名称
          appointmentMode:'',           //预约方式
          appointmentDate:'',           //预约时间
          applicationTime:new Date(),   //申请时间
          unloadingPlace:'',            //卸货地点
          residueAmount:"",             //剩余数量
          companyCode:"",               //公司代码
          companyName:"",               //公司名称
          appointmentPeriod:"",         //时间段
          state:"",                     //状态
          weighingWeight:"",            //过磅重量
          tareWeight:"",                //皮重
          netWeight:"",                 //净重
          confirmDate:"",               //确认时间
          deliveryPacket:"",            //送货包数
          brokenPacket:"",              //破包数量
          receiptsQty:"",               //实收吨数
          debtQty:"",                   //共欠数量
          linkMan:""
        },         
        dialogUnloadLocationItem:[
          'C区','涌口'
        ],    //卸货地点数据
        tempSearch:{},                  //保存查询数据
        stateItem:[                     //状态数组
          {code:0,value:"正常"},
          {code:1,value:"禁用"},
        ],
        
        //表格操作
        selectRowIndex:"",              //选中行

      }
    },

    beforeMount(){
      this.gridOptions = gridOptions();
      this.gridOptions.context = {
            componentParent:this
        };
    let setCellStyle = (pramas) => {
      const { state } = pramas.data;
      // 未审批粉色；已卸货紫色;其余默认黑色
      if (state === 0) {
        return { color: "#FF34B3" };
      } else if(state === 5){
        return { color: "#cc33ff" };
      }
    };
      this.columnDefs=[
        {
            headerName:"预约日期",
            field:"appointmentDate",
            width:120,
            checkboxSelection: true,
            headerCheckboxSelection: true,
            cellStyle:setCellStyle
            },
        {headerName:"预约时段",field:"appointmentPeriod",width:110,cellStyle:setCellStyle},
        {headerName:"车号",field:"carNum",width:100,cellStyle:setCellStyle},
        {headerName:"第几车",field:"whichCar",width:90,cellStyle:setCellStyle},
        {headerName:"送货数量",field:"quantity",width:90,cellStyle:setCellStyle},
        {headerName:"卡号/柜号",field:"cabinet",width:110,cellStyle:setCellStyle},
        {headerName:"采购订单",field:"purchaseOrderNo",width:110,cellStyle:setCellStyle},
        {headerName:"供应商名称",field:"supplierName",width:230,cellStyle:setCellStyle},
        {headerName:"货物名称",field:"goodsName",width:120,cellStyle:setCellStyle},
        {headerName:"卸货地点",field:"unloadingPlace",width:130,cellStyle:setCellStyle},
        {headerName:"联系人",field:"linkMan",width:190,cellStyle:setCellStyle},
        {
        headerName:"状态",
          field:"state",
          width:80,
          cellRenderer:params=>{
            this.appointStateItem.forEach(item=>{
              if(item.code==params.value){
                 params.value=item.name
              }
            })
            return params.value
          },
          cellStyle:setCellStyle
        },
        {headerName:"订单剩余数量",field:"residueAmount",width:120,cellStyle:setCellStyle},
        {headerName:"运输方式",field:"transport",width:100,cellStyle:setCellStyle},
        {headerName:"司机",field:"driver",width:110,cellStyle:setCellStyle},
        {headerName:"司机电话",field:"driverPhone",width:120,cellStyle:setCellStyle},
        {headerName:"确认时间",field:"confirmDate",width:150,cellStyle:setCellStyle},
        {headerName:"过磅重量(kg)",field:"weighingWeight",width:120,cellStyle:setCellStyle},
        {headerName:"皮重(kg)",field:"tareWeight",width:100,cellStyle:setCellStyle},
        {headerName:"净重(kg)",field:"netWeight",width:100,cellStyle:setCellStyle},
        {headerName:"收条编号",field:"receiptNo",width:150,cellStyle:setCellStyle},
        {headerName:"收条打印次数",field:"printTimes",width:120,cellStyle:setCellStyle},
        {headerName:"公司代码",field:"companyCode",width:110,cellStyle:setCellStyle},
        {headerName:"微信确认时间",field:"weixinDate",width:120,cellStyle:setCellStyle},
      ]

    },
    created(){
      this.searchCompany();
      this.queryAppointList();
      let company_code=this.$global.user.bukrs;
      this.queryUnloadLocation(company_code);
    },
    watch:{
        'appointform.unloadingPlace':{
            handler(newValue,oldValue){
                this.areaLinkMans = this.unloadLocationInputItem.filter(item => {
                    return newValue == item.unloadAddree
                })
            },
            immediate:true
        }
    },
    methods:{
    /**
     * @vuese
     * 送货地点改变回调
     * @arg 参数为改变后的值
    */
    addressChange (val) {
        this.appointform.linkMan = ''
        this.areaLinkMans = this.unloadLocationInputItem.filter(item => {
            return val == item.unloadAddree
        })
    },
      //公司查询
      searchCompany () {
          var obj = {
            state: 0
          };
          var that =this;
          this.$http.getCompanyInfo(obj).then(function (response) {
            var data = response.data;
            if (data.code == "1" && data.total>0) {
              let companyData=data.rows.splice(0);
              that.companyItem=companyData;
            } else {
              that.$alert(data.info, {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            }
          }).catch((err) => {
         
          })
      },
      

      //获取时间段
        queryAppointTime(value){
         let params={
            companyCode:value,
            pageSize:10000,
            current:1
          }
        this.$http.queryAppointTime(params)
        .then((res)=>{
          let data=res.data;
          if(data.code==1){
            let originData = data.rows.splice(0);
            originData.forEach(item=>{
              this.appointPeriodItem.push(item.timeFrame)
            })
          }else{
            that.$alert(data.info, {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
            });
          }
        })
        .catch((err)=>{
        
        })
      },

      // 公司切换
      companyChange(val){
        this.appointPeriodItem=[];
        this.unloadLocationItem=[];
        this.unloadLocationInputItem=[];
        this.queryAppointTime(val);
        this.queryUnloadLocation(val);
        this.companyItem.forEach(item=>{
          if(item.companyCode==val){
            this.appointform.companyName=item.companyName
          }
        })
      },
      //清除
      clearCompaney(){
        this.appointform.companyName="";
        this.appointPeriodItem=[];
        this.unloadLocationItem=[];
        this.unloadLocationInputItem=[];
      },
      // 卸货地点
      queryUnloadLocation(value){
          let params={
            companyCode:value,
            pageSize:10000,
            current:1
          }
          this.areaLinkMans = []
        this.$http.queryUnloadLocation(params)
        .then(res=>{
            let data=res.data;
            if(data.code==1){
              let originData = data.rows.splice(0); 
              this.unloadLocationInputItem = originData 
              let result = {};
              for(let i=0;i<originData.length;i++){
                  result[originData[i].unloadAddree]=originData[i];
              }
              for(let item in result){
                  this.unloadLocationItem.push(result[item]);
              }
            }else{
              this.unloadLocationItem=[];
              this.unloadLocationInputItem=[];
                this.$alert(data.info, {
                  showClose:false,
                  confirmButtonText: "确定",
                  customClass: "alertFailure"
                });
            }
        })
        .catch((error)=>{
            this.unloadLocationItem=[];
            this.unloadLocationInputItem=[];
        })
      },

      //获取剩余车数
      queryRemainQty(params){
        let obj={
          purchaseOrderNo:params
        }
        this.$http.queryRemainQty(obj).then(res=>{
          let data =res.data;
          if(data.code==1 &&data.total>0){
            let originData = data.rows;
            this.$set(this.appointform,'supplierName',originData.supplierName)
            this.$set(this.appointform,'supplierCode',originData.supplierCode)
            this.$set(this.appointform,'residueAmount',originData.residueAmount)
            // this.appointform.supplierName=originData.supplierName;      //供应商
            // this.appointform.supplierCode=originData.supplierCode;      //供应商代码
            // this.appointform.residueAmount=originData.residueAmount ;   //剩余车数
          }else{
            this.appointform.supplierName="";
            this.appointform.residueAmount="";
            this.appointform.supplierCode="";
            this.$alert(data.info,{
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            })
          }
        }).catch(err=>{
          this.appointform.supplierName="";
          this.appointform.residueAmount="";
          this.appointform.supplierCode="";
        })
      },
      //采购订单号变更
      purchaseOrderChange(value){
        this.queryRemainQty (value);
      },
      // 清除采购订单
      purchaseOrderClear(){
        this.appointform.supplierName="";      //供应商
        this.appointform.supplierCode="";      //供应商代码
        this.appointform.residueAmount="";     //剩余车数
      },
      //选中事件
      handleSelectionChange(event){
        this.multipleSelection = [];
        let selection = event.api.getSelectedRows();
        selection.forEach((item, index) => {
          this.multipleSelection.push(item);
        });
      },
      //保存查询参数
      saveTempCondition(){
          this.tempSearch.companyCode=this.companyCode;
          this.tempSearch.carNo = this.carNo;
          this.tempSearch.appointPeriod=this.appointPeriod;
          this.tempSearch.transportType=this.transportType;
          this.tempSearch.appointState=this.appointState;
          this.tempSearch.unloadLocation=this.unloadLocation;
          this.tempSearch.purchaseOrderNo=this.purchaseOrderNo;
          this.tempSearch.supplier=this.supplier;
          this.tempSearch.cabinetNoAndCardNo=this.cabinetNoAndCardNo;
          this.tempSearch.startDate=this.startDate;
          this.tempSearch.endDate=this.endDate;
      },
        //获取查询参数
      getTempCondition(){
          this.companyCode=this.tempSearch.companyCode;
          this.carNo = this.tempSearch.carNo;
          this.appointPeriod=this.tempSearch.appointPeriod;
          this.transportType=this.tempSearch.transportType;
          this.appointState=this.tempSearch.appointState;
          this.unloadLocation=this.tempSearch.unloadLocation;
          this.purchaseOrderNo=this.tempSearch.purchaseOrderNo;
          this.supplier=this.tempSearch.supplier;
          this.cabinetNoAndCardNo=this.tempSearch.cabinetNoAndCardNo;
          this.startDate=this.tempSearch.startDate;
          this.endDate=this.tempSearch.endDate;
      },

      //查询首页
      queryAppointList(type){
          let params = {
            pageSize:this.pageSize,
            current:this.currentPage
          }
          if(this.companyCode){
            params.companyCode=this.companyCode;
          }
          if(this.appointPeriod){
            params.appointmentPeriod=this.appointPeriod;
          }
          if(this.transportType){
            params.transport=this.transportType;
          }
          if(this.appointState){
            params.state=this.appointState;
          }
          if(this.unloadLocation){
            params.unloadingPlace=this.unloadLocation;
          }
          if(this.carNo){
            params.likeParams.carNum=this.carNo;
          }
          if(this.purchaseOrderNo){
            params.likeParams.purchaseOrderNo=this.purchaseOrderNo;
          }
          if(this.supplier){
            params.likeParams.supplierName=this.supplier;
          }
          if(this.cabinetNoAndCardNo){
            params.likeParams.cabinet=this.cabinetNoAndCardNo;
          }
          if(this.startDate){
            params.startDate=this.startDate;
          }
          if(this.endDate){
            params.endDate=this.endDate;
          }
          if(this.endDate && this.endDate<this.startDate){
            this.$alert("开始日期不能大于结束日期",{
              showClose:false,
              comfirmButton:"确定",
              customClass:"alertPrompt"
            })
          }
        if(type === 1){
              params.current = 1;
              this.currentPage = 1;
          }
        this.queryDisabled=true;
        this.loading=true;
        this.$http.queryAppointLis(params).then(res=>{
          this.saveTempCondition();
          this.queryDisabled=false;
          this.loading=false;
          let data = res.data;
          if(data.code==1){
            let originData=data.rows.splice(0);
            this.rowData=originData
            this.totalPage=data.total;  
          }else{
            this.rowData=[];
            this.totalPage=0;
            that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
          }
        }).catch(error=>{
          this.queryDisabled=false;
          this.loading=false;
          this.$alert("查询失败",{
            showClose:false,
            comfirmButton:"确定",
            customClass:"alertFailure"
          })
        })
      },

      // 表格聚焦行
      rowFocus(event){
          this.selectRowIndex=event.rowIndex;
      },
      //选择分页重新回填数据
      paginationFillCondition(type){
          this.getTempCondition();
          this.queryAppointList(type);
      },
      //条数变化
      handleSizeChange(value){
        this.pageSize = value;
        this.paginationFillCondition(1);
      },
      //当前页变化
      handleCurrentChange(value){
        this.currentPage = value;
        this.paginationFillCondition(0);
      },
      //向前一页
      handlePrevClick(value){
        this.currentPage = value;
        this.paginationFillCondition(0);
      },
      // 向后一页
      handleNextClick(value){
        this.currentPage = value;
        this.paginationFillCondition(0);
      },

      //预约通过/退回
      appointmentPassOrReturn(type){
        let appointmentPassedSelectRow=this.gridOptions.api.getSelectedRows();
        let ids=[];
        let stateData=true
        if(appointmentPassedSelectRow.length>=1){
          appointmentPassedSelectRow.forEach(item=>{
            ids.push(item.id);
            if(item.state!=0){
              return stateData=false
            }
          })
          if(stateData==false){
            this.$alert("请确认状态为未审批",{
              showClose: false,
              confirmButtonText: "确定",
              customClass: "alertPrompt"
            })
            return
          } 
        }
        else{
          this.$alert("请选择要编辑的数据",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
        let params={
          confirmer:this.$global.user.loginName,
          confirmerId: this.$global.user.id,
          ids: ids
        }
        if(type==1){
          params.state= 1
        }else if(type==2){
          params.state= 2
        }
        this.$http.passOrReturnAppointment(params).then(res=>{
          let data = res.data;
          if(data.code==1){
            this.$message({
              message:data.info,
              type:"success"
            })
            this.multipleSelection = [];
            this.queryAppointList();
          }else{
            this.$alert(data.info, {
              showClose: false,
              confirmButtonText: "确定",
              customClass: "alertFailure"
            });
          }
        }).catch(error=>{
          this.$alert("操作失败",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertFailure"
          })
        })
      },
      //通过websocket获取地磅数
      getWeight(id,type){
        let params={
          value:id,
          type:type
        }
        this.websocketInit(id);
        this.$http.getWeight(params).then(res=>{
          let data =res.data;
          if(data.code!=1){
            this.$alert(data.info,{
              showClose:false,
              confirmButtonText: "确定",
              customClass: "alertFailure"
            })
          }
        }).catch(error=>{
          this.$alert("同步失败,请检查网络",{
              showClose:false,
              confirmButtonText: "确定",
              customClass: "alertFailure"
            })
        })
      },
      //链接webSocket
      websocketInit(id){
        let myId ="";
        if(this.dialogEdit=='confirmUnload'){
          myId="xhyyxh"+id
        }else if(this.dialogEdit=='confirmGoIn'){
          myId='xhyy'+id
        }
        this.wsClient = new websocketClient({
          systemEnv: constant.appenv,   //dev
          // userId: constant.userId,      //59280
          userId: myId,    
          systemCode: "srm",
          loginTimestamp: new Date().getTime(),
          fanoutCallback: msg => {
            let res = JSON.parse(msg.body);
            this.$notify({
              title: `系统通知`,
              message: res.message,
              type: "warning",
              duration: 0,
              position: "bottom-right"
            });
          },
          topicCallback: msg => {
            let res = JSON.parse(msg.body);
            console.log("ws res:", msg);
            if(this.dialogEdit=='confirmGoIn'){
              this.appointform.weighingWeight=res.Weight;
            }else if(this.dialogEdit=='confirmUnload'){
              this.appointform.tareWeight=res.Weight;
              this.appointform.netWeight=Number(this.appointform.weighingWeight)-Number(this.appointform.tareWeight);
            }
            if(typeof(res.type)!="undefined"){
              switch (res.type){
                case 'total':{
                  this.updateNotice(res.value?res.value:0)
                  break;
                }
              }
            }
          }
        });
        this.wsClient.connect();
      },
      // 确认进场
      confirmArrivalBtn(){
        let selectRow=this.gridOptions.api.getSelectedRows();
        if(selectRow[0].state!==1){
          this.$alert("请确认状态为已通过",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
        this.dialogFormVisible=true;
        this.weighingReadonly=false;
        this.tareReadonly=true;
        this.netReadonly=true;
        this.dialogEdit='confirmGoIn';
        this.dialogTitle="确认进场";
        this.appointform.tareWeight=selectRow[0].tareWeight;
        this.getWeight(selectRow[0].id,0);
      },
      //确认进场
      confirmArrival(){
        let confirmSelectRow=this.gridOptions.api.getSelectedRows();
        let id=confirmSelectRow[0].id;
        let linkMan="";
        let unloadingPlace="";
          //过磅重量
        if(this.appointform.weighingWeight==""){
          this.$alert("过磅重不能为空",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
         //联系人
        if(confirmSelectRow[0].linkMan==""){
          this.$alert("请选择联系人",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }else{
          linkMan=confirmSelectRow[0].linkMan;
        }
        //卸货地点
        if(confirmSelectRow[0].unloadingPlace==""){
          this.$alert("请选择卸货地点",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }else{
          unloadingPlace=confirmSelectRow[0].unloadingPlace;
        }
        let params={
          confirmer: this.$global.user.loginName,
          confirmerId: this.$global.user.id,
          weighingWeight : this.appointform.weighingWeight,
          linkMan: linkMan,
          unloadingPlace: unloadingPlace  ,
          id: id

        }
        this.confirmGoIn=true;
        this.$http.confirmDelivery(params).then(res=>{
          this.dialogFormVisible=false;
          this.wsClient.disconnent();  //断开webSocket
          this.confirmGoIn=false;
          this.weighingReadonly=false;
          this.tareReadonly=false;
          this.netReadonly=false;
          let data =res.data;
          if(data.code==1){
            this.$message({
              message:data.info,
              type:"success"
            })
            this.appointform.weighingWeight="";
            this.appointform.weighingWeight="";
            this.appointform.tareWeight="";
            this.appointform.netWeight="";
            this.multipleSelection = [];
            this.queryAppointList();
          }else{
            
            this.appointform.weighingWeight="";
            this.appointform.tareWeight="";
            this.appointform.netWeight="";
            this.multipleSelection = [];
            this.$alert(data.info, {
              showClose: false,
              confirmButtonText: "确定",
              customClass: "alertFailure"
            });
          }
        }).catch(error=>{
          this.dialogFormVisible=false;
          this.appointform.weighingWeight="";
          this.appointform.tareWeight="";
          this.appointform.netWeight="";
          this.weighingReadonly=false;
          this.tareReadonly=false;
          this.netReadonly=false;
          this.confirmGoIn=false;
          this.$alert(error.data.info,{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertFailure"
          })
        })
      },
      inputKeyup(value){
        if(value==undefined || value<0){
          value=0;
        }
        this.appointform.netWeight=Number(this.appointform.weighingWeight)-Number(value);
         //净重不能小于0
        if(this.appointform.weighingWeight<=value){
          this.$alert("过磅重不能小于皮重",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        this.appointform.tareWeight=0;
        this.appointform.netWeight=0;
          return
        }
      },
      //卸货确认
      confirmUnloadingBtn(){
        let selectRow=this.gridOptions.api.getSelectedRows();
         //状态
        if(selectRow[0].state!=3){
          this.$alert("请确认状态为已到达",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
        this.dialogFormVisible=true;
        this.weighingReadonly=true;
        this.tareReadonly=false;
        this.netReadonly=true;
        this.dialogEdit='confirmUnload';
        this.dialogTitle="卸货确认";
        this.appointform.weighingWeight=selectRow[0].weighingWeight;
        if(this.appointform.tareWeight==""){
          this.appointform.tareWeight=0;
        }
        this.inputKeyup();
        this.getWeight(selectRow[0].id,1)         //调用websocket
      },
      //卸货确认
      confirmUnloading(){
        let unloadingSelectRow=this.gridOptions.api.getSelectedRows();
        let id="";
        //ID
        if(unloadingSelectRow[0].id==""){
          this.$alert("ID不能为空",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }else{
          id=unloadingSelectRow[0].id;
        }
        //皮重
        if(this.appointform.tareWeight==""){
          this.$alert("皮重不能为空",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
       
        let params={    
              confirmer: this.$global.user.loginName,
              confirmerId: this.$global.user.id,
              tareWeight:this.appointform.tareWeight,               //皮重
              deliveryPackedQty:this.appointform.deliveryPacket,    //送货包数
              baleBrokenQty:this.appointform.brokenPacket,          //破包数量
              receiveTunnage:this.appointform.receiptsQty,          //实收吨数
              oweQty:this.appointform.debtQty,                      //共欠数量
              id:id        
        }
        if(this.appointform.netWeight>0){
          params.tareWeight=this.appointform.tareWeight             //净重
        }else{
          this.$alert("请核对重量",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
          return
        }
        this.confirmUnload=true;
        this.$http.unloadComfim(params).then(res=>{
            let data = res.data ;
            this.dialogFormVisible=false;
            this.wsClient.disconnent();  //断开webSocket
            this.confirmUnload=false;
            this.weighingReadonly=false;
            this.tareReadonly=false;
            this.netReadonly=false;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
              })
              this.appointform.weighingWeight="";
              this.appointform.tareWeight="";
              this.appointform.netWeight="";
              this.multipleSelection = [];
              this.queryAppointList();
            }else{
              this.dialogFormVisible=false;
              this.confirmUnload=false;
              this.appointform.weighingWeight="";
              this.appointform.tareWeight="";
              this.appointform.netWeight="";
              this.multipleSelection = [];
              this.$alert("卸货确认失败",{
                showClose:false,
                confirmButtonText:"确定",
                customClass:"alertFailure"
              })
            }
        }).catch(error=>{
            this.dialogFormVisible=false;
            this.appointform.weighingWeight="";
            this.appointform.tareWeight="";
            this.appointform.netWeight="";
            this.multipleSelection = [];
            this.confirmUnload=false;
            this.weighingReadonly=false;
            this.tareReadonly=false;
            this.netReadonly=false;
            this.$alert(error.data.info,{
              showClose:false,
              confirmButtonText:"确定",
              customClass:"alertFailure"
            })
          })
      },
      //打印批准书/收条
      print(tempType){
        let receiptPrintingRow=this.gridOptions.api.getSelectedRows();
        if (receiptPrintingRow.length <=0) {
          this.$alert("请选择需要打印的收条数据", {
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          });
          return;
        }else if(receiptPrintingRow.length >1){
          this.$alert("只能打印一条收条数据", {
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          });
          return;
        }
        let type = tempType=="receiptPrint"? 1: 0;
        if (constant.userAgent == 'web') {
          fileStream.getFileInfo(tempType).then((data) => {
            startPrint({
              basicUrl: ENV['srm'], //权限接口地址
              reportUrl: `${ENV['srm']}download2?viewPath=${data.url}&groupName=${data.groupName}`, //打印模板地址
              dataUrl: 'srmTblAppointment/printAppointment', //数据接口地址
              params: {
                'token': '',
                'id': receiptPrintingRow[0].id,
                'type': type
              }, //参数对象
              printType: 'preview'     // 打印方式print直接打印，preview先预览
            })
          }).catch((err) => {
            Msg.showErrorMsg(err)
          })
        } else {
          this.getPrintData(receiptPrintingRow[0], tempType)
        }
      },
      // 客户端打印
      getPrintData (row, type) {
        const ENCRYPT_KEY = 'lesso2128token11'
        waterfall([
          (callback) => {
            this.$http.getPrintToken().then(({ data }) => {
              let token = aes.encrypt(data, ENCRYPT_KEY);
              callback(null, token);
            }).catch(err => {
              Msg.showErrorMsg(err.data.message)
            })
          },
          (token, callback) => {
            this.$http.getPrintAccountData({ id: row.id, token: token }).then(({ data }) => {
              let printData = data.rows
              callback(null, printData)
            }).catch(err => {
              Msg.showErrorMsg(err.data.message)
            })
          },
          (printData, callback) => {
            if (constant.printType == 'local') {
              ipcRenderer.send("IPCR_M_PRINTLOCAL", {
                type: type,
                rows: printData,
                optype: 'pr',
                isdefaultp: 1//控制是否直接打印,0为选择打印、1为直接打印
              });
            } else {
              fileStream.getFileInfo(type, printData).then((data) => {
                ipcRenderer.send("IPCR_M_PRINT", {
                  type: type,
                  rows: printData,
                  groupName: data.groupName,
                  optype: "pr",
                  url: data.url,
                  isdefaultp: 1//控制是否直接打印,0为选择打印、1为直接打印
                })
              }).catch((err) => {
                Msg.showErrorMsg(err)
              })
            }
          }
        ])
      },

      // 弹框取消
      dialogCancel(){
        this.$refs['appointform'].resetFields()
        setTimeout(()=>{
            this.dialogFormVisible=false;
            this.appointform.unloadingPlace=''
        },100)
      },

       //双击编辑
      doubleClickedEdit(event){
        let selectRow=event.data;
        this.dialogFormVisible=true;
        this.dialogEdit='edit';
        this.dialogTitle="编辑"
        this.appointform={...selectRow};
        this.appointform.applicationTime=new Date();               //申请日期
        this.appointform.confirmDate=this.appointform.modifyDate   // 确认日期
        if(this.appointform.weChatFanId==null){
            this.appointform.appointmentMode="现场排队"
        }else{
          this.appointform.appointmentMode="微信预约"
        }
        this.appointStateItem.forEach(item=>{                        //状态
          if(item.code==this.appointform.state){
            this.appointform.state=item.name;
          }
        })
        this.queryRemainQty (this.appointform.purchaseOrderNo);   //获取剩余数量/供应商名称
          
      },
       //现场开单
      onSiteBilling(){
        this.dialogFormVisible=true;
        this.appointDateReadOnly=false;   //预约日期只读
        this.dialogEdit='siteBill';
        this.dialogTitle="现场开单";
        this.appointform.appointmentMode="现场排队";
        this.appointform.applicationTime=new Date();
        this.appointform.companyCode=this.$global.user.butxt;   //公司代码
        this.appointform.companyName=this.$global.user.butxt;   //公司名称
        let _companyCode=this.$global.user.bukrs;               
        this.queryUnloadLocation(_companyCode);
        this.queryAppointTime(_companyCode)
        this.appointform.unloadingPlace=this.unloadLocationItem[0].unloadAddree;   //卸货地点
        this.appointPeriodItem=[];
        this.unloadLocationItem=[];
      },
      //编辑
      editAppointList(){
        this.appointDateReadOnly=true;     //预约日期只读
        let selectRow=this.gridOptions.api.getSelectedRows();
        if(selectRow.length==1){
          if(selectRow[0].state!==0){
            this.$alert("请确认状态为未审批",{
              showClose: false,
              confirmButtonText: "确定",
              customClass: "alertPrompt"
            })
            return
          }
          this.dialogFormVisible=true;
          this.dialogEdit='edit';
          this.dialogTitle="编辑";
          this.appointform={...selectRow[0]};
          this.appointform.applicationTime=new Date();   //申请日期
  
          // 预约方式
          if(this.appointform.weChatFanId==null){
            this.appointform.appointmentMode="现场排队"
          }else{
            this.appointform.appointmentMode="微信预约"
          }
          this.appointform.confirmDate=this.appointform.modifyDate      // 确认日期
          this.appointStateItem.forEach(item=>{                        //状态
            if(item.code==this.appointform.state){
              this.appointform.state=item.name;
            }
          })
        }else if(selectRow.length>1){
          this.$alert("只能选择一行数据编辑",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }else{
          this.$alert("请选择要编辑的数据",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }

      },
      
      // 弹框确认(现场开单/编辑)
      dialogEditSubmit(){
        let msg = "";
        let xreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;  //新能源车牌（8位）
        let creg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;    //非新能源车牌（7位）
        let dialogCarNoLen= this.$refs["carNum"].textLength;
        let goodsNameLen= this.$refs["goodsName"].textLength;
        if(this.appointform.carNum==""){
          msg="车号不能为空";
        }else if((!xreg.test(this.appointform.carNum) && !creg.test(this.appointform.carNum))){
           msg="请输入正确的车号";
        }else if(this.appointform.driver==""){
           msg="司机姓名不能为空"
        }else if(dialogCarNoLen>50){
           msg="司机姓名不能大于50个字段";
        }else if(this.appointform.transport==""){
          msg="运输方式不能为空";
        }else if(this.appointform.driverPhone==""){
          msg="司机电话不能为空";
        }else if(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.appointform.driverPhone)==false){
          msg="请输入正确手机号码";
        }else if(this.appointform.purchaseOrderNo==""){
          msg="采购订单号不能为空";
        }else if(this.appointform.goodsName==""){
          msg="货物名称不能为空";
        }else if(goodsNameLen>100){
           msg="货物名称不能大于100字符";
        }else if(this.appointform.appointmentDate==""){
            msg="预约时间不能为空";
        }else if(this.appointform.unloadingPlace==""){
            msg="卸货地点不能为空";
        } else if(this.appointform.linkMan==""){
            msg="卸货地点联系人不能为空";
        } else if(this.appointform.companyCode==""){
            msg="公司名称不能为空";
        }else if(this.appointform.appointmentPeriod==""){
            msg="预约时段不能为空";
        }
        if(msg!=""){
            this.$alert(msg, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
            });
            return;
        }
        let params = {
          appointmentDate: this.appointform.appointmentDate,      //预约日期
          appointmentMode: this.appointform.appointmentMode,      //预约方式
          appointmentPeriod:this.appointform.appointmentPeriod,   //预约时段
          cabinet:[this.appointform.cabinet],                     //柜号/卡号           
          carNum: this.appointform.carNum,                        //车号
          companyCode: this.appointform.companyCode,              //公司代码
          companyName: this.appointform.companyName,              //公司名称
          creator: this.$global.user.loginName,                   //创建者
          creatorId: this.$global.user.id,                        //创建ID
          driver: this.appointform.driver,                        //司机
          driverPhone: this.appointform.driverPhone,              //司机号码
          goodsName:this.appointform.goodsName,                   //货物名称
          modifyer: this.$global.user.loginName,                  
          modifyerId: this.$global.user.id,
          purchaseOrderNo:this.appointform.purchaseOrderNo,       //采购订单
          quantity:this.appointform.quantity,                     //送货数量
          remark: this.appointform.goodsName,                     //货物名称
          residueAmount:this.appointform.residueAmount,           //剩余数量
          supplierCode: this.appointform.supplierCode,            //供应商代码
          supplierName: this.appointform.supplierName,            //供应商名称
          transport: this.appointform.transport,                  //运输方式
          unloadingPlace: this.appointform.unloadingPlace,         //卸货地点
          linkMan:this.appointform.linkMan
        }
        
        // 现场开单
        if(this.dialogTitle=="现场开单"){
          this.siteBillDisabled=true;
          this.dialogSiteBillSubmit=true;
          this.dialogFormVisible=true;
          if(this.appointform.companyCode==this.$global.user.butxt){
            params.companyCode=this.$global.user.bukrs;
          }
          this.$http.saveAppointLis(params).then(res=>{
            this.siteBillDisabled=false;
            this.dialogFormVisible=false;
            let data = res.data;
            if(data.code==1){
                this.$message({
                  message:data.info,
                  type:"success"
              })
              this.appointform={};
              this.dialogSiteBillSubmit=false;
              this.multipleSelection = [];
              this.queryAppointList();
              
              }else{
                this.appointform={};
                this.dialogSiteBillSubmit=false;
                this.multipleSelection = [];
                this.$alert(data.info, {
                  showClose: false,
                  confirmButtonText: "确定",
                  customClass: "alertFailure"
                });
              }
          }).catch(error=>{
            this.siteBillDisabled=false;
            this.dialogFormVisible=false;
            this.appointform={};
            this.dialogSiteBillSubmit=false;
            this.multipleSelection = [];
            this.supplier="";
            this.$alert(error.data.info,{
              confirmButtonText:"确定",
              customClass:"alertFailure"
            })
          })
        }
        //编辑
        else if(this.dialogTitle="编辑"){
          params.weighingWeight=this.appointform.weighingWeight          //过磅重量
          params.tareWeight= this.appointform.tareWeight                 //皮重
          params.netWeight= this.appointform.netWeight                   //净重   
          params.confirmDate=this.appointform.confirmDate                //确认时间
          params.id=this.appointform.id                                  //编辑ID
          this.dialogFormVisible=true;
          this.editConfirm=true;
          this.$http.editAppointLis(params).then(res=>{
            this.dialogFormVisible=false;
            let data =res.data;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
              })
              this.appointform={};
              this.editConfirm=false;
              this.multipleSelection = [];
              this.queryAppointList();
            }else{
              this.appointform={};
              this.editConfirm=false;
              this.multipleSelection = [];
              this.$alert(data.info, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertFailure"
              });
            }
          }).catch(error=>{
            this.appointform={};
            this.editConfirm=false;
            this.multipleSelection = [];
            this.dialogFormVisible=false;
            this.$alert(error.data.info,{
              confirmButtonText:"确定",
              customClass:"alertFailure"
            })
          })
        }
      },

      destroyed () {
        // this.wsClient.disconnent();  
      }
    }
}
</script>

<style lang="less" >
.m-view-appointmentList{
  .el-row{
    height: 40px;
    line-height: 40px;
    .el-form-item{
      margin-bottom: 0px;
    }
  }
  .m-agTable{
    top: 150px !important;
    .ag-table{
      height: 100%;
      
    }
    .el-input__inner{
      height: 24px !important;
      line-height: 24px !important;
    }
    .el-input__icon{
      line-height: 24px !important;
    }
   
  }
  
}

 
</style>